factor out Annex.GitShaKey
authorJoey Hess <joeyh@joeyh.name>
Mon, 3 Mar 2025 15:08:36 +0000 (11:08 -0400)
committerJoey Hess <joeyh@joeyh.name>
Mon, 3 Mar 2025 15:09:28 +0000 (11:09 -0400)
Annex/Export.hs
Annex/GitShaKey.hs [new file with mode: 0644]
Annex/Import.hs
Command/Export.hs
git-annex.cabal

index 60039ef3b9bfabfc73ce475a26fddda1446d143d..4ce30e2fcaf0054ce49feccb8a208d9853d950d8 100644 (file)
@@ -11,16 +11,13 @@ module Annex.Export where
 
 import Annex
 import Annex.CatFile
+import Annex.GitShaKey
 import Types
-import Types.Key
 import qualified Git
 import qualified Types.Remote as Remote
 import Git.Quote
 import Messages
 
-import Data.Maybe
-import qualified Data.ByteString.Short as S (fromShort, toShort)
-
 -- From a sha pointing to the content of a file to the key
 -- to use to export it. When the file is annexed, it's the annexed key.
 -- When the file is stored in git, it's a special type of key to indicate
@@ -31,31 +28,6 @@ exportKey sha = mk <$> catKey sha
        mk (Just k) = k
        mk Nothing = gitShaKey sha
 
--- Encodes a git sha as a key. This is used to represent a non-annexed
--- file that is stored on a special remote, which necessarily needs a
--- key.
---
--- This is not the same as a SHA1 key, because the mapping needs to be
--- bijective, also because git may not always use SHA1, and because git
--- takes a SHA1 of the file size + content, while git-annex SHA1 keys
--- only checksum the content.
-gitShaKey :: Git.Sha -> Key
-gitShaKey (Git.Ref s) = mkKey $ \kd -> kd
-       { keyName = S.toShort s
-       , keyVariety = OtherKey "GIT"
-       }
-
--- Reverse of gitShaKey
-keyGitSha :: Key -> Maybe Git.Sha
-keyGitSha k
-       | fromKey keyVariety k == OtherKey "GIT" =
-               Just (Git.Ref (S.fromShort (fromKey keyName k)))
-       | otherwise = Nothing
-
--- Is a key storing a git sha, and not used for an annexed file?
-isGitShaKey :: Key -> Bool
-isGitShaKey = isJust . keyGitSha
-
 warnExportImportConflict :: Remote -> Annex ()
 warnExportImportConflict r = do
        isimport <- Remote.isImportSupported r
diff --git a/Annex/GitShaKey.hs b/Annex/GitShaKey.hs
new file mode 100644 (file)
index 0000000..1413039
--- /dev/null
@@ -0,0 +1,41 @@
+{- Encoding a git sha as a Key
+ -
+ - Copyright 2017-2025 Joey Hess <id@joeyh.name>
+ -
+ - Licensed under the GNU AGPL version 3 or higher.
+ -}
+
+{-# LANGUAGE OverloadedStrings #-}
+
+module Annex.GitShaKey where
+
+import Types
+import Types.Key
+import qualified Git
+
+import Data.Maybe
+import qualified Data.ByteString.Short as S (fromShort, toShort)
+
+-- Encodes a git sha as a Key. This is used to represent a non-annexed
+-- file. For example, when storing a git sha on a special remote.
+--
+-- This is not the same as a SHA1 key, because the mapping needs to be
+-- bijective, also because git may not always use SHA1, and because git
+-- takes a SHA1 of the file size + content, while git-annex SHA1 keys
+-- only checksum the content.
+gitShaKey :: Git.Sha -> Key
+gitShaKey (Git.Ref s) = mkKey $ \kd -> kd
+       { keyName = S.toShort s
+       , keyVariety = OtherKey "GIT"
+       }
+
+-- Reverse of gitShaKey
+keyGitSha :: Key -> Maybe Git.Sha
+keyGitSha k
+       | fromKey keyVariety k == OtherKey "GIT" =
+               Just (Git.Ref (S.fromShort (fromKey keyName k)))
+       | otherwise = Nothing
+
+-- Is a key storing a git sha, and not used for an annexed file?
+isGitShaKey :: Key -> Bool
+isGitShaKey = isJust . keyGitSha
index 2e86df920d5d3611e656f7fb90a8692eb37b446a..2d2526a544cb2a6b2b19c96dfd545c90621cf473 100644 (file)
@@ -38,12 +38,12 @@ import qualified Annex
 import Annex.Link
 import Annex.LockFile
 import Annex.Content
-import Annex.Export
 import Annex.RemoteTrackingBranch
 import Annex.HashObject
 import Annex.Transfer
 import Annex.CheckIgnore
 import Annex.CatFile
+import Annex.GitShaKey
 import Annex.VectorClock
 import Annex.SpecialRemote.Config
 import Command
index b4acaac401da6e33c4f6befba736776036254b67..3be1a67c93a20b4cd8466b3259c25c23ad476be9 100644 (file)
@@ -26,6 +26,7 @@ import Types.Remote
 import Types.Export
 import Annex.Export
 import Annex.Content
+import Annex.GitShaKey
 import Annex.Transfer
 import Annex.CatFile
 import Annex.FileMatcher
index 88203be956a10c3613ccd69fe3df2f516fe9f7ae..2123b73663a52ed5e64ea1ce2d05d3763357a3ba 100644 (file)
@@ -562,6 +562,7 @@ Executable git-annex
     Annex.FileMatcher
     Annex.Fixup
     Annex.GitOverlay
+    Annex.GitShaKey
     Annex.HashObject
     Annex.Hook
     Annex.Import